package com.spring.sperapc.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import com.spring.security.core.context.JedisCont;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.iputil.HttpClientUtils;
import com.spring.security.web.dao.JedisCache;
import com.spring.security.web.dao.UserMapper;
import com.spring.security.web.model.User;
import com.spring.sperapc.web.dao.FamilyMemberMapper;
import com.spring.sperapc.web.dao.TracelogboMapper;
import com.spring.sperapc.web.dao.UserDetailMapper;
import com.spring.sperapc.web.model.FamilyMember;
import com.spring.sperapc.web.model.TracelogboWithBLOBs;
import com.spring.sperapc.web.service.FamilyMemberService;
import com.spring.sperapc.web.service.UserCommonService;
import com.spring.tools.MyTools;

/**
 * Created by wojunyu on 2015/8/12.
 */
@Service
public class FamilyMemberServiceImpl implements FamilyMemberService{
    @Resource
    private FamilyMemberMapper familyMemberMapper;

    @Resource
    private UserMapper userMapper;
    
    @Resource
    private UserDetailMapper userDetailMapper;
    
    @Resource
    private UserCommonService userCommonService;
    
    @Resource
    private TracelogboMapper tracelogboMapper;
    
    @Resource
    private JedisCache jedisCache;
    
    @Override
    public int deleteByPrimaryKey(@Param(value = "id") String id) {
        return familyMemberMapper.deleteByPrimaryKey(id);
    }

    /**
     * 新增家庭成员
     */
    @Override
    public String insertSelective(FamilyMember record) {
		User s = new User();
		s.setUsername(record.getUsername());
		List<User> list = userMapper.selectByWhereStr(s);
		if ((ApplicationUtils.isNotNull(list)) && (list.size() > 0)) {
			return "-1";
		}
		
		String count=userCommonService.getFamilyRoleCount(record);
		if(count.equals("0")){
			return "2";
		}
		
		record.setEntityAvailableState("10");
        int fm = familyMemberMapper.insertSelective(record);
        if(fm==0){
        	return "0";
        }else{
        	return "1";
        }
    }

    @Override
    public List<FamilyMember> selectByFamilyId(Map m) {
        return familyMemberMapper.selectByFamilyId(m);
    }

    @Override
    public int updateByPrimaryKeySelective(FamilyMember record,String token,HttpServletRequest request) {
    	if(record.getPhone()!=null&&!record.equals("")){
    		String cUserID=record.getC_user_id();

    		//String newPhone=record.getPhone();
    		
    		TracelogboWithBLOBs tl = MyTools.getLog(jedisCache, token, request);
    		tl.setEntitytypename("c_family_member");
    		tl.setEntityid(cUserID);
    		tl.setLogcategory(6);
    		tl.setLogdescription(null);
    		int effect=tracelogboMapper.insert(tl);
    		System.out.println(effect);
    	}

    	int ret = 0;
    	/**
    	 * 判断角色，除宝宝外，一个家庭内其余角色不可重复
    	 */
    	String count=userCommonService.getFamilyRoleCountByID(record);
		if(count.equals("0")){
			return 2;
		}
    	
    	int fmi = familyMemberMapper.updateByPrimaryKeySelective(record);

        if(fmi==1){
            ret = 1;
        }
        return ret;
    }
}
